/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include "MqcpRegReport.h"
#include "SystemMqcpTask.h"


static MqcpDataDef *MqcpRegReportReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpRegReportRespProcess(MqcpMessageDef *Message);

uint16_t *RegReportAddr = NULL;

void RegReportInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		RegReportReq,
		MqcpRegReportReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		RegReportResp,
		MqcpRegReportRespProcess
	};
	while(addRouter(routeResp));
}
/*
* 上报类型 
*								 0x01代表上报AI
*							   0x02代表变化上报
*								 0x0E代表上报4Byte
*								 0x0F代表上报2Byte
*/
uint8_t ReportType = 0;

MqcpDataDef *MqcpRegReportReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = 0;
	
	MqcpData->Size = 0;
	uint16_t MqcpSize = 0;//发送的data数
	
	rt_memset(MqcpData->MqcpMessage.MsgId,0,12);MqcpData->Size = MqcpData->Size +12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = RegReportReq;MqcpData->Size = MqcpData->Size + 1;
	
	if(0x0F == ReportType)
	{//上报2BYTE
		MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;
		
		uint16_t Data2ReportSize = 0;
		
		for(uint16_t i = 0; i < Data2Size; i++)
		{
			if(0 == Data2Byte[i].Address)
			{
				continue;
			}
			Data2ReportSize++;
			Data2Byte[i].Data = AddrGetValue(Data2Byte[i].Address - 1);
			MqcpData->MqcpMessage.Data[MqcpSize] = Data2Byte[i].Type;
			MqcpData->MqcpMessage.Data[MqcpSize+2] = Data2Byte[i].Address>>8;
			MqcpData->MqcpMessage.Data[MqcpSize+1] = Data2Byte[i].Address&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data2Byte[i].Address-1)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data2Byte[i].Address-1)&0xff;MqcpData->Size = MqcpData->Size + 5;MqcpSize = MqcpSize + 5;
		}
		MqcpData->MqcpMessage.Data[1] = 0;
		MqcpData->MqcpMessage.Data[0] = Data2ReportSize;
	}
	else if(0x0E == ReportType)
	{//上报4BYTE
		MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;
		
		uint16_t Data4ReportSize = 0;
		
		for(uint16_t i = 0; i < Data4Size; i++)
		{
			if(0 == Data4Byte[i].Address)
			{
				continue;
			}
			Data4ReportSize++;
			Data4Byte[i].Data[0] = AddrGetValue(Data4Byte[i].Address - 1);
			Data4Byte[i].Data[1] = AddrGetValue(Data4Byte[i].Address - 1 + 1);
			MqcpData->MqcpMessage.Data[MqcpSize] = Data4Byte[i].Type;
			MqcpData->MqcpMessage.Data[MqcpSize+2] = Data4Byte[i].Address>>8;
			MqcpData->MqcpMessage.Data[MqcpSize+1] = Data4Byte[i].Address&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data4Byte[i].Address-1)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data4Byte[i].Address-1)&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize+6] = AddrGetValue(Data4Byte[i].Address-1+1)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize+5] = AddrGetValue(Data4Byte[i].Address-1+1)&0xff;MqcpData->Size = MqcpData->Size + 7;MqcpSize = MqcpSize + 7;
		}
		MqcpData->MqcpMessage.Data[1] 	= 0;
		MqcpData->MqcpMessage.Data[0] = Data4ReportSize;
	}
	else if(0x01 == ReportType)
	{//主动上报
		ReportState = 1;//开始检测模块
		
		uint16_t AI_Size = 0;
		MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;
    for(uint16_t i = 0; i < Data2Size; i++)
    {
			if(0 == Data2Byte[i].Address)
			{
				continue;
			}
			if(MQCP_WRREG_ADDR_RUN + MQCP_WRREG_SIZE_RUN + 0 >= Data2Byte[i].Address)
			{
				AI_Size++;
				Data2Byte[i].Data = AddrGetValue(Data2Byte[i].Address-1);
				MqcpData->MqcpMessage.Data[MqcpSize] = Data2Byte[i].Type;
				MqcpData->MqcpMessage.Data[MqcpSize+2] = Data2Byte[i].Address>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+1] = Data2Byte[i].Address&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data2Byte[i].Address-1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data2Byte[i].Address-1)&0xff;
				MqcpData->Size = MqcpData->Size + 5;MqcpSize = MqcpSize + 5;
			}
    }
		for(uint16_t i = 0; i < Data4Size; i++)
		{
			if(0 == Data4Byte[i].Address)
			{
				continue;
			}
			if(MQCP_WRREG_ADDR_RUN + MQCP_WRREG_SIZE_RUN + 1 >= Data4Byte[i].Address)
			{
				AI_Size++;
				Data4Byte[i].Data[0] = AddrGetValue(Data4Byte[i].Address-1);
				Data4Byte[i].Data[1] = AddrGetValue(Data4Byte[i].Address-1 + 1);
				MqcpData->MqcpMessage.Data[MqcpSize] = Data4Byte[i].Type;
				MqcpData->MqcpMessage.Data[MqcpSize+2] = (Data4Byte[i].Address)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+1] = (Data4Byte[i].Address)&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data4Byte[i].Address-1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data4Byte[i].Address-1)&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+6] = AddrGetValue(Data4Byte[i].Address-1+1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+5] = AddrGetValue(Data4Byte[i].Address-1+1)&0xff;MqcpData->Size = MqcpData->Size + 7;MqcpSize = MqcpSize + 7;
			}
		}
		MqcpData->MqcpMessage.Data[1] = 0;
		MqcpData->MqcpMessage.Data[0] = AI_Size;
	}
	else if(0x02 == ReportType)
	{//变化上报
		uint16_t Change_Size = 0;
		
		rt_memset(Data2Report,0,Data2Size);
		rt_memset(Data4Report,0,Data4Size);
		
		MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;
		for(uint16_t i = 0; i < Data2Size; i++)
		{
			if(0 == Data2Byte[i].Address)
			{
				continue;
			}
			if(AddrGetValue(Data2Byte[i].Address-1) != Data2Byte[i].Data)
			{
				Data2Report[i] = 1;
				
				Change_Size++;
//				Data2Byte[i].Data = AddrGetValue(Data2Byte[i].Address-0);
				MqcpData->MqcpMessage.Data[MqcpSize] = Data2Byte[i].Type;
				MqcpData->MqcpMessage.Data[MqcpSize+2] = Data2Byte[i].Address>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+1] = Data2Byte[i].Address&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data2Byte[i].Address-1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data2Byte[i].Address-1)&0xff;
				
				if(MQCP_WRREG_ADDR_RUN + MQCP_WRREG_SIZE_RUN + 0 >= Data2Byte[i].Address)
				{
					DIDOState[i] = ((MqcpData->MqcpMessage.Data[MqcpSize+4])<<8)|(MqcpData->MqcpMessage.Data[MqcpSize+3]);
				}
				MqcpData->Size = MqcpData->Size + 5;MqcpSize = MqcpSize + 5;
			}
		}
		for(uint16_t i = 0; i < Data4Size; i++)
		{
			if(0 == Data4Byte[i].Address)
			{
				continue;
			}
			if((Data4Byte[i].Address >= MQCP_WRREG_ADDR_SET)&&((AddrGetValue(Data4Byte[i].Address-1) != Data4Byte[i].Data[0])||(AddrGetValue(Data4Byte[i].Address-1 + 1) != Data4Byte[i].Data[1])))
			{
				Data4Report[i] = 1;
				
				Change_Size++;
//				Data4Byte[i].Data = AddrGetValue(Data4Byte[i].Address-0);
				MqcpData->MqcpMessage.Data[MqcpSize] = Data4Byte[i].Type;
				MqcpData->MqcpMessage.Data[MqcpSize+2] = (Data4Byte[i].Address)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+1] = (Data4Byte[i].Address)&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+4] = AddrGetValue(Data4Byte[i].Address-1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+3] = AddrGetValue(Data4Byte[i].Address-1)&0xff;
				MqcpData->MqcpMessage.Data[MqcpSize+6] = AddrGetValue(Data4Byte[i].Address-1+1)>>8;
				MqcpData->MqcpMessage.Data[MqcpSize+5] = AddrGetValue(Data4Byte[i].Address-1+1)&0xff;MqcpData->Size = MqcpData->Size + 7;MqcpSize = MqcpSize + 7;
			}
		}
		MqcpData->MqcpMessage.Data[1] = 0;
		MqcpData->MqcpMessage.Data[0] = Change_Size;
	}
	MqcpData->MqcpMessage.Len[1] = MqcpSize>>8;
	MqcpData->MqcpMessage.Len[0] = MqcpSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}

MqcpDataDef *MqcpRegReportRespProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	if(0x01 == ReportType)
	{
		ReportState = 0;//检测模块正常
	}
	
	if(0x02 == ReportType)
	{
		for(uint16_t i = 0; i < Data2Size; i++)
		{
			if(0 != Data2Report[i])
			{
				Data2Byte[i].Data = ((i < DIMAXSIZE + DOMAXSIZE) ? DIDOState[i] : AddrGetValue(Data2Byte[i].Address-1));
			}
		}
		for(uint16_t i = 0; i < Data4Size; i++)
		{
			if(0 != Data4Report[i])
			{
				Data4Byte[i].Data[0] = AddrGetValue(Data4Byte[i].Address-1);
				Data4Byte[i].Data[1] = AddrGetValue(Data4Byte[i].Address-1 + 1);
			}
		}
	}

	MqcpData->isNeedResp = Message->Sync;
	MqcpData->Size = 0;
	
	return MqcpData;
}



/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
